/*
 * @(#)ListDmsPersonalStorageSetupAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsPersonalStorageSetup;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.core.DmsPersonalStorageSetupManager;
import com.dcivision.dms.core.RootOperationManager;
import com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.UserErrorConstant;
import com.dcivision.user.bean.MtmUserGroupUserRole;
import com.dcivision.user.bean.MtmUserRecordUserGroup;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserMember;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  ListDmsPersonalStorageSetupAction.java
  This class is for user's storage limit setup Function.
    @author          Jenny Li
    @company         DCIVision Limited
    @creation date   24/11/2004
    @version         $Revision: 1.21.2.2 $
 */

public class ListDmsPersonalStorageSetupAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.21.2.2 $";

  protected String parentID = "";
  protected String rootID = "";
  protected final static String MESSAGE_RECORD_INSERT = "common.message.record_inserted";

  public ListDmsPersonalStorageSetupAction() {
    super();
    this.setListName("memberList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return ("com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return (SystemFunctionConstant.DMS_ADMIN);
  }

  public void setPageTitle(HttpServletRequest request,HttpServletResponse response,ActionForm form,ActionMapping mapping, ActionForward actionForward){
    if(GlobalConstant.NAV_MODE_LIST.equals(actionForward.getName())){
      String extendTitle = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale(), "dms.label.personal_storage_limit");
      request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
      request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
      return;
    }
  }
  /**
   *  Method execute() - generaic function of result list handling
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    //  Set the function code for current action.
	SessionContainer sessionContainer = this.getSessionContainer(request);
	Connection conn = conn = this.getConnection(request);
	RootOperationManager rootOperationManager = new RootOperationManager(sessionContainer, conn);
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    AbstractSearchForm searchForm=(AbstractSearchForm)form;
    if(UserMember.OPMODE_ADD_USER.equals(actionForm.getOpMode())){
      //set user's storage limit.
      try{
        this.addUserRecord(mapping,form,request,response);
        this.commit(request);
        addMessage(request,MESSAGE_RECORD_INSERT);
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      this.getListData(mapping,searchForm,request,response);
      actionForm.setOpMode("");
      actionForm.setUserRecordID("");
      actionForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else if(UserMember.OPMODE_ADD_GROUP.equals(actionForm.getOpMode())){
      //set group's storage limit.
      try{
        this.addUserGroup(mapping,form,request,response);
        this.commit(request);
        addMessage(request,MESSAGE_RECORD_INSERT);
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      this.getListData(mapping,searchForm,request,response);
      actionForm.setOpMode("");
      actionForm.setUserGroupID("");
      actionForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else if(UserMember.OPMODE_ADD_ROLE.equals(actionForm.getOpMode())){
      //set role's storage limit.
      try{
        this.addUserRole(mapping,form,request,response);
        this.commit(request);
        addMessage(request,MESSAGE_RECORD_INSERT);
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      this.getListData(mapping,searchForm,request,response);
      actionForm.setOpMode("");
      actionForm.setUserRoleID("");
      actionForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else if(DmsPersonalStorageSetup.OP_MODE_SEARCH.equals(actionForm.getOpMode())){
    	Integer userID = TextUtility.parseIntegerObj(actionForm.getUserRecordID());
        DmsRootDAObject rootDAO = new DmsRootDAObject(sessionContainer, conn);
        List rootList = rootDAO.getRootByOwnerIDAndType(userID, DmsRoot.PERSONAL_ROOT, GlobalConstant.RECORD_STATUS_ACTIVE);
        log.debug(" Both parentID and rootID = null, rootList size = " + rootList.size());
        DmsRoot rootPoint = null;
        if (rootList.size() == 0) {
          // Create default root for user without root point
          log.debug("no default root, going to create one");
          rootPoint = rootOperationManager.createDefaultPersonalRootPointForUser(userID);
          this.commit(request);
        }
      //search each user's storage limit.
      try{
        this.searchStorageLimit(mapping,form,request,response);
      }catch (ApplicationException appEx){
        this.rollback(request);
        handleApplicationException(request,appEx);
      }
      return mapping.findForward(DmsPersonalStorageSetup.NAV_MODE_SEARCH);
    }else if(Utility.isEmpty(actionForm.getOpMode()) && DmsPersonalStorageSetup.NAV_MODE_SEARCH.equals(actionForm.getNavMode())){
      return mapping.findForward(DmsPersonalStorageSetup.NAV_MODE_SEARCH);
    }else{
      //this.getListData(mapping,form,request,response);
      return super.execute(mapping,form,request,response);
    }
  }

  /**
   *  Method getListData() - retrieve the appropriate item list for current page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.getListData(mapping,form,request,response);
    SessionContainer sessionContainer=this.getSessionContainer(request);
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    Connection conn=this.getConnection(request);
    UserRecordDAObject userRecordDAO=new UserRecordDAObject(sessionContainer,conn);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    List memberList=new ArrayList();
    memberList=(List)request.getAttribute(this.getListName());
    String memberName="";
    for(int i=0;i<memberList.size();i++){
      DmsPersonalStorageSetup dmsPersonalStorageSetup=(DmsPersonalStorageSetup)memberList.get(i);
      if(DmsPersonalStorageSetup.MEMBER_TYPE_USER.equals(dmsPersonalStorageSetup.getMemberType())){
        UserRecord userRecord=(UserRecord)userRecordDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
        if(!Utility.isEmpty(userRecord)){
          memberName=userRecord.getFullName();
        }

      }else if(DmsPersonalStorageSetup.MEMBER_TYPE_GROUP.equals(dmsPersonalStorageSetup.getMemberType())){
        UserGroup userGroup=(UserGroup)userGroupDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
        memberName=userGroup.getGroupName();
      }else if(DmsPersonalStorageSetup.MEMBER_TYPE_ROLE.equals(dmsPersonalStorageSetup.getMemberType())){
        UserRole userRole=(UserRole)userRoleDAO.getObjectByID(dmsPersonalStorageSetup.getMemberID());
        memberName=userRole.getRoleName();
      }
      dmsPersonalStorageSetup.setMemberName(memberName);
    }
    actionForm.setUserGroupID(null);
    actionForm.setUserRecordID(null);
    actionForm.setUserRoleID(null);
    actionForm.setUnlimit(null);
    request.setAttribute(this.getListName(),memberList);
  }

  /**
   * this function is to add user record.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserRecord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    Integer userRecordID=TextUtility.parseIntegerObj(actionForm.getUserRecordID());
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    DmsRootDAObject dmsRootDAO=new DmsRootDAObject(sessionContainer,conn);
    DmsPersonalStorageSetup dmsPersonalStorageSetup=new DmsPersonalStorageSetup();
    dmsPersonalStorageSetup.setMemberID(userRecordID);
    dmsPersonalStorageSetup.setMemberType(DmsPersonalStorageSetup.MEMBER_TYPE_USER);
    if(!Utility.isEmpty(actionForm.getStorageLimit())){
      dmsPersonalStorageSetup.setStorageLimit(TextUtility.parseDoubleObj(actionForm.getStorageLimit()));
    }
    if("Y".equals(actionForm.getUnlimit())){
      dmsPersonalStorageSetup.setStorageLimit(TextUtility.parseDoubleObj("0"));
    }
    //check has double record?
    DmsPersonalStorageSetup oldDmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userRecordID,DmsPersonalStorageSetup.MEMBER_TYPE_USER);
    if(!Utility.isEmpty(oldDmsPersonalStorageSetup)){
      throw new ApplicationException(UserErrorConstant.DUPLICATE_RECORD);
    }else{
      dmsPersonalStorageSetupDAO.insertObject(dmsPersonalStorageSetup);
    }
    //update user's storage limit in dms root.
    DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(userRecordID,GlobalConstant.RECORD_STATUS_ACTIVE);
    dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
    dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
  }

  /**
   * this function is to add group.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    Integer userGroupID=TextUtility.parseIntegerObj(actionForm.getUserGroupID());
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);

    DmsPersonalStorageSetup dmsPersonalStorageSetup=new DmsPersonalStorageSetup();
    dmsPersonalStorageSetup.setMemberID(userGroupID);
    dmsPersonalStorageSetup.setMemberType(DmsPersonalStorageSetup.MEMBER_TYPE_GROUP);
    if(!Utility.isEmpty(actionForm.getStorageLimit())){
      dmsPersonalStorageSetup.setStorageLimit(TextUtility.parseDoubleObj(actionForm.getStorageLimit()));
    }else{
      dmsPersonalStorageSetup.setStorageLimit(new Double(0));
    }
    if("Y".equals(actionForm.getUnlimit())){
      dmsPersonalStorageSetup.setStorageLimit(TextUtility.parseDoubleObj("0"));
    }
    //check has double record?
    DmsPersonalStorageSetup oldDmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userGroupID,DmsPersonalStorageSetup.MEMBER_TYPE_GROUP);
    if(!Utility.isEmpty(oldDmsPersonalStorageSetup)){
      throw new ApplicationException(UserErrorConstant.DUPLICATE_RECORD);
    }else{
      dmsPersonalStorageSetupDAO.insertObject(dmsPersonalStorageSetup);
    }

    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    DmsPersonalStorageSetupManager dmsPersonalStorageSetupManager=new DmsPersonalStorageSetupManager(sessionContainer,conn);
    DmsRootDAObject dmsRootDAO=new DmsRootDAObject(sessionContainer,conn);
    List userRecordList=mtmUserRecordUserGroupDAO.getUserIDListByUserGroupID(userGroupID);
    //update user's storage limit in dms root.
    for(int i=0;i<userRecordList.size();i++){
      Integer userRecordID=(Integer)userRecordList.get(i);
      DmsPersonalStorageSetup userPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userRecordID,DmsPersonalStorageSetup.MEMBER_TYPE_USER);
      DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(userRecordID,GlobalConstant.RECORD_STATUS_ACTIVE);
      if(Utility.isEmpty(userPersonalStorageSetup) && !dmsPersonalStorageSetupManager.checkHasStorageRole(userRecordID)){
        if(!Utility.isEmpty(dmsRoot)){
          List groupStorageList=dmsPersonalStorageSetupDAO.getStorageGroupListByUserID(userRecordID);
          if(groupStorageList.size()>0){
            Double oldStorageLimit=((DmsPersonalStorageSetup)groupStorageList.get(0)).getStorageLimit();
            if(!"0.0".equals(dmsPersonalStorageSetup.getStorageLimit().toString())){
              if(dmsPersonalStorageSetup.getStorageLimit().equals(oldStorageLimit)){
                dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
                dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
              }
            }else{
              dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
              dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
            }
          }else{
            dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
            dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
          }
        }
      }
    }
  }

  /**
   * this function is to add role.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    Integer userRoleID=TextUtility.parseIntegerObj(actionForm.getUserRoleID());
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    DmsPersonalStorageSetup dmsPersonalStorageSetup=new DmsPersonalStorageSetup();
    dmsPersonalStorageSetup.setMemberID(userRoleID);
    dmsPersonalStorageSetup.setMemberType(DmsPersonalStorageSetup.MEMBER_TYPE_ROLE);
    if(!Utility.isEmpty(actionForm.getStorageLimit())){
      dmsPersonalStorageSetup.setStorageLimit(TextUtility.parseDoubleObj(actionForm.getStorageLimit()));
    }
    if("Y".equals(actionForm.getUnlimit())){
      dmsPersonalStorageSetup.setStorageLimit(TextUtility.parseDoubleObj("0"));
    }
    //check has double record?
    DmsPersonalStorageSetup oldDmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userRoleID,DmsPersonalStorageSetup.MEMBER_TYPE_ROLE);
    if(!Utility.isEmpty(oldDmsPersonalStorageSetup)){
      throw new ApplicationException(UserErrorConstant.DUPLICATE_RECORD);
    }else{
      dmsPersonalStorageSetupDAO.insertObject(dmsPersonalStorageSetup);
    }
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    DmsPersonalStorageSetupManager dmsPersonalStorageSetupManager=new DmsPersonalStorageSetupManager(sessionContainer,conn);
    DmsRootDAObject dmsRootDAO=new DmsRootDAObject(sessionContainer,conn);
    List userRecordList=mtmUserRecordUserRoleDAO.getUserIDListByUserRoleID(userRoleID);
    List roleList=new ArrayList();
    roleList.add(0,userRoleID);

    //start get the user not belong current role directly.
    List groupList=mtmUserGroupUserRoleDAO.getListByUserRoleAry(roleList);
    for(int j=0;j<groupList.size();j++){
      MtmUserGroupUserRole mtmUserGroupUserRole=(MtmUserGroupUserRole)groupList.get(j);
      List groupUserList=mtmUserRecordUserGroupDAO.getUserIDListByUserGroupID(mtmUserGroupUserRole.getUserGroupID());
      for(int k=0;k<groupUserList.size();k++){
        Integer uerRecordID=(Integer)groupUserList.get(k);
        userRecordList.add(uerRecordID);
      }
    }

    java.util.Hashtable hst=new java.util.Hashtable();
    List newUserIDList=new ArrayList();
    for(int j=0;j<userRecordList.size();j++){
      Integer userID=(Integer)userRecordList.get(j);
      hst.put(userID,userID);
    }
    if(hst.size()>0){
      java.util.Enumeration e = hst.elements();
      int j = 0;
      newUserIDList.add(0,e.nextElement());
      while (e.hasMoreElements()) {
        j++;
        newUserIDList.add(j,e.nextElement());
      }
    }//end get user not belong current rolt.

    //udpate user's storage limit in dms root.
    for(int i=0;i<newUserIDList.size();i++){
      Integer userRecordID=(Integer)newUserIDList.get(i);
      DmsPersonalStorageSetup userDmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(userRecordID,DmsPersonalStorageSetup.MEMBER_TYPE_USER);
      if(Utility.isEmpty(userDmsPersonalStorageSetup)){
        DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(userRecordID,GlobalConstant.RECORD_STATUS_ACTIVE);
        if(!Utility.isEmpty(dmsRoot)){
          List roleStorageList=dmsPersonalStorageSetupDAO.getStorageRoleListByUserID(userRecordID);
          if(roleStorageList.size()>0){
            Double oldStorageLimit=((DmsPersonalStorageSetup)roleStorageList.get(0)).getStorageLimit();
            if(!"0.0".equals(dmsPersonalStorageSetup.getStorageLimit().toString())){
              if(dmsPersonalStorageSetup.getStorageLimit().equals(oldStorageLimit)){
                dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
                dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
              }
            }else{
              dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
              dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
            }
          }else{
            dmsRoot.setStorageLimit(dmsPersonalStorageSetup.getStorageLimit());
            dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
          }
        }
      }
    }
  }

  /**
   * this function is for search a user's personal storage limit.
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void searchStorageLimit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    Integer userRecordID=TextUtility.parseIntegerObj(actionForm.getUserRecordID());
    DmsRootDAObject dmsRootDAO=new DmsRootDAObject(sessionContainer,conn);
    DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(userRecordID,GlobalConstant.RECORD_STATUS_ACTIVE);
    String storageLimit="";
    if("0.0".equals(dmsRoot.getStorageLimit().toString())){
      storageLimit="unlimit";
    }else{
      storageLimit=TextUtility.formatStorageSize(new Double(dmsRoot.getStorageLimit().toString()),"MB");
    }
    actionForm.setStorageLimit(storageLimit);
  }

  /**
   *  Method deleteListData() - delete the item(s) from listing page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws ApplicationException the deleteion failed
   */
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListDmsPersonalStorageSetupForm actionForm=(ListDmsPersonalStorageSetupForm)form;
    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);

    String[] basicSelectedID=form.getBasicSelectedID();
    for(int i=0;i<basicSelectedID.length;i++){
      Integer selectedID=TextUtility.parseIntegerObj(basicSelectedID[i]);
      //delete record in dms_personal_storage_setup.
      DmsPersonalStorageSetup dmsPersonalStorageSetup=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.deleteObjectByID(selectedID);

      if(DmsPersonalStorageSetup.MEMBER_TYPE_USER.equals(dmsPersonalStorageSetup.getMemberType())){
        this.upateDmsRoot(dmsPersonalStorageSetup.getMemberID(),request);
      }else if(DmsPersonalStorageSetup.MEMBER_TYPE_GROUP.equals(dmsPersonalStorageSetup.getMemberType())){
        List groupList=new ArrayList();
        groupList.add(0,dmsPersonalStorageSetup.getMemberID());
        List userList=mtmUserRecordUserGroupDAO.getListByUserGroupAry(groupList,GlobalConstant.RECORD_STATUS_ACTIVE);
        for(int j=0;j<userList.size();j++){
          MtmUserRecordUserGroup mtmUserRecordUserGroup=(MtmUserRecordUserGroup)userList.get(j);
          this.upateDmsRoot(mtmUserRecordUserGroup.getUserRecordID(),request);
        }
      }else if(DmsPersonalStorageSetup.MEMBER_TYPE_ROLE.equals(dmsPersonalStorageSetup.getMemberType())){
        List roleList=new ArrayList();
        roleList.add(0,dmsPersonalStorageSetup.getMemberID());
        List userList=mtmUserRecordUserRoleDAO.getUserIDListByUserRoleID(dmsPersonalStorageSetup.getMemberID());
        //get the user not belong current role directly.
        List groupList=mtmUserGroupUserRoleDAO.getListByUserRoleAry(roleList);
        for(int j=0;j<groupList.size();j++){
          MtmUserGroupUserRole mtmUserGroupUserRole=(MtmUserGroupUserRole)groupList.get(j);
          List groupUserList=mtmUserRecordUserGroupDAO.getUserIDListByUserGroupID(mtmUserGroupUserRole.getUserGroupID());
          for(int k=0;k<groupUserList.size();k++){
            Integer userRecordID=(Integer)groupUserList.get(k);
            userList.add(userRecordID);
          }
        }

        java.util.Hashtable hst=new java.util.Hashtable();
        List newUserIDList=new ArrayList();
        for(int j=0;j<userList.size();j++){
          Integer userID=(Integer)userList.get(j);
          hst.put(userID,userID);
        }
        if(hst.size()>0){
          java.util.Enumeration e = hst.elements();
          int j = 0;
          newUserIDList.add(0,e.nextElement());
          while (e.hasMoreElements()) {
            j++;
            newUserIDList.add(j,e.nextElement());
          }
        }
        for(int j=0;j<newUserIDList.size();j++){
          Integer newUserID=(Integer)newUserIDList.get(j);
          this.upateDmsRoot(newUserID,request);
        }
      }
    }

  }

  /**
   * update user's Storage Limit.
   * @param selectedID
   * @param request
   * @throws ApplicationException
   */
  protected void upateDmsRoot(Integer selectedID,HttpServletRequest request) throws ApplicationException{
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);

    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
    DmsRootDAObject dmsRootDAO=new DmsRootDAObject(sessionContainer,conn);
    Double storageLimit=new Double(SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_ROOT_STORAGE_LIMIT));
    DmsRoot dmsRoot=dmsRootDAO.getPersonalRootByOwnerIDAndType(selectedID,GlobalConstant.RECORD_STATUS_ACTIVE);
    //update user's storage limit in dms root.
    if(!Utility.isEmpty(dmsRoot)){
      DmsPersonalStorageSetup userStorage=(DmsPersonalStorageSetup)dmsPersonalStorageSetupDAO.getByMemberIDMemberType(selectedID,DmsPersonalStorageSetup.MEMBER_TYPE_USER);
      if(!Utility.isEmpty(userStorage)){
        storageLimit=userStorage.getStorageLimit();
      }else{
        List roleStorageList=dmsPersonalStorageSetupDAO.getStorageRoleListByUserID(selectedID);
        if(roleStorageList.size()>0){
          storageLimit=((DmsPersonalStorageSetup)roleStorageList.get(0)).getStorageLimit();
        }else{
          List groupStorageList=dmsPersonalStorageSetupDAO.getStorageGroupListByUserID(selectedID);
          if(groupStorageList.size()>0){
            storageLimit=((DmsPersonalStorageSetup)groupStorageList.get(0)).getStorageLimit();
          }
        }
      }
      if(!storageLimit.equals(dmsRoot.getStorageLimit())){
        dmsRoot.setStorageLimit(storageLimit);
        dmsRoot=(DmsRoot)dmsRootDAO.updateObject(dmsRoot);
      }
    }
  }

}
